﻿package com.dream.testtubebaby.activity;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.dream.testtubebaby.R;
import com.dream.testtubebaby.utils.StatusBarCompat;

public class SelectPhotoActivity extends Activity {
	public static final String TAG = "SelectPhotoActivity";
	public static final String GET_PICTURE_KEY = "picture_data";
	public static final String IS_PHOTO_ZOOM_KEY = "is_photo_zoom";
	public static final int REQUEST_PICTURE_CODE = 0x123;
	public static final int HEAD_TYPE_RES = 1;
	public static final int HEAD_TYPE_FILE = 2;
	private TextView mTextViewPicture;
	private TextView mTextViewCapture;
	private TextView mTextViewCancel;

	private String tempFileName;
	private String fileName;
	public static final String USER_PHOTO_DIR = "/testtubebaby/";
	public static final String USER_PHOTO_FILE_NAME = "user_photo_file_name.png";
	public static final String APP_FILE_NAME = "TestTubeBaby.apk";
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.select_picture_view);
		StatusBarCompat.compat(this, getResources().getColor(R.color.status_bar_color));
		initIntent();
		initContentView();
	}

	private void initIntent() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
		String str = format.format(new Date());
		String filePath = Environment.getExternalStorageDirectory() + USER_PHOTO_DIR;
		checkFolderExists(filePath);
		fileName = filePath + USER_PHOTO_FILE_NAME;
		tempFileName = filePath + str + ".temp.png";
		Log.i(TAG, "fileName=" + fileName);
	}

	private boolean checkFolderExists(String strFolder) {
		Log.i(TAG, "checkFolderExists() strFolder=" + strFolder);
		File file = new File(strFolder);
		if (!file.exists()) {
			if (file.mkdir()) {
				return true;
			} else
				return false;
		}
		return true;
	}

	private void initContentView() {
		mTextViewPicture = (TextView) findViewById(R.id.user_head_picture);
		mTextViewCapture = (TextView) findViewById(R.id.user_head_capture);
		mTextViewCancel = (TextView) findViewById(R.id.user_head_cancel);
		mTextViewPicture.setOnClickListener(mOnClickListener);
		mTextViewCapture.setOnClickListener(mOnClickListener);
		mTextViewCancel.setOnClickListener(mOnClickListener);
	}

	private OnClickListener mOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.user_head_picture:
				goPicture();
				break;
			case R.id.user_head_capture:
				goCapture();
				break;
			case R.id.user_head_cancel:
				finish();
				break;
			default:
				break;
			}
		}
	};

	protected void goPicture() {
		Intent intent = new Intent(Intent.ACTION_PICK, null);
		intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
				"image/*");
		startActivityForResult(intent, 1);
	}

	protected void goCapture() {
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		intent.putExtra(MediaStore.EXTRA_OUTPUT,
				Uri.fromFile(new File(tempFileName)));
		startActivityForResult(intent, 2);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		// 如果是直接从相册获取
		case 1: {
			if (data != null) {
				startPhotoZoom(data.getData());
			}
		}
			break;
		// 如果是调用相机拍照时
		case 2:
			startPhotoZoom(Uri.fromFile(new File(tempFileName)));
			break;
		// 取得裁剪后的图片
		case 3:
			if (data != null) {
				setPicToView(data);
			}
			break;
		default:
			break;

		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	/**
	 * 裁剪图片方法实现
	 * 
	 * @param uri
	 */
	public void startPhotoZoom(Uri uri) {
		Log.i(TAG, "startPhotoZoom(" + uri.toString() + ")");
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		// 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
		intent.putExtra("crop", "true");
		// aspectX aspectY 是宽高的比例
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		// outputX outputY 是裁剪图片宽高
		intent.putExtra("outputX", 200);
		intent.putExtra("outputY", 200);
		intent.putExtra("return-data", true);
		startActivityForResult(intent, 3);
	}

	private void setPicToView(Intent picdata) {
		Bundle extras = picdata.getExtras();
		if (extras != null) {
			Bitmap photo = extras.getParcelable("data");
			savePicture(photo);
		}
	}

	private void savePicture(Bitmap photo) {
		Log.i(TAG, "savePicture()");
		File file = new File(fileName);
		if (file.exists()) {
			file.delete();
		}
		try {
			FileOutputStream out = new FileOutputStream(file);
//			BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
			photo.compress(Bitmap.CompressFormat.PNG, 80, out);
			out.flush();
			out.close();
//			CompressFormat format= Bitmap.CompressFormat.PNG;
//			int quality = 100;
//			OutputStream stream = new FileOutputStream(fileName);
//			photo.compress(format, quality, stream);
			
			Intent intent = new Intent();
			intent.putExtra(GET_PICTURE_KEY, photo);
			setResult(Activity.RESULT_OK, intent);
			finish();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void finish() {
		super.finish();
		overridePendingTransition(R.anim.main_enter, R.anim.sub_exit);
	}
}